package com.ruoyi.wx.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.wx.entity.PtExp;
import com.ruoyi.wx.service.IPtExpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.Oneway;
import java.io.File;

/**
 * <p>
 * 实验表 前端控制器
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/wx")
@Api(value = "实验展示接口", tags = {"实验展示接口测试"})
public class PtExpController {
    @Autowired
    private IPtExpService service;

    @Autowired
    private ISysDeptService sysDeptService;

    @ApiOperation("展示所有实验")
    @GetMapping("/getExpAll")
    public AjaxResult getExpAll() {
        return AjaxResult.success(service.list(Wrappers.lambdaQuery(PtExp.class)
            .select(PtExp::getId,PtExp::getExpName)
            .eq(PtExp::getStatus,'0')
        ));
    }

    @ApiOperation("获取实验内容的pdf")
    @GetMapping("/getExpPdf")
    public AjaxResult getExpPdf(Long expId) {
        PtExp byId = service.getById(expId);
        return AjaxResult.success("操作成功",byId.getPdfUrl());
    }

    @ApiOperation("上传实验内容pdf")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = File.class, required = true),
    })
    @PostMapping("/uploadExpPdf")
    public AjaxResult uploadExpPdf(@RequestPart("file") MultipartFile file, Long expId) {
        if(file == null) {
            return AjaxResult.error("实验报告不能为空");
        }else {
            String originalfileName = file.getOriginalFilename();
            String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
            if (!".pdf".equals(suffix)) {
                return AjaxResult.error("实验报告类型为pdf");
            }
            service.uploadExpPdf(file, expId);
            return AjaxResult.success();
        }
    }

}
